package NoticeController;

import com.alibaba.fastjson.JSONObject;
import com.renjun.smweb.commons.util.Encrypt;
import com.renjun.smweb.commons.util.Format;
import com.renjun.smweb.controller.NoticeController.NoticeController;
import com.renjun.smweb.dao.pojo.LoginInfo;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.mock.web.MockHttpServletRequest;
import org.springframework.mock.web.MockHttpServletResponse;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;

import java.util.HashMap;
import java.util.Map;

/**
 * Created by huamin on 2017/2/23.
 * 公告管理M9
 */
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration({"classpath*:/spring-mvc.xml", "classpath*:/spring-mybatis.xml"})
public class NoticeControllerTestCase {

    @Autowired
    private NoticeController noticeController;

    private MockHttpServletRequest request;

    private MockHttpServletResponse response;

    @Before
    public void setUp() {
        request = new MockHttpServletRequest();
        request.getSession().setAttribute("userinfo", getLoginInfo());
    }

    @Test
    public void testQueryNotice() {
        System.out.println("查询公告列表");
        //封装参数
        Map<String, Object> params = new HashMap<>();
        params.put("title", "德玛西");
        params.put("userid", "");
        request.setContent(JSONObject.toJSONString(params).getBytes());
        //查询
        JSONObject rs = noticeController.queryNotice(request, response);
        String jiamStr = (String) rs.get("response");
        String jiemStr = Encrypt.jiem(jiamStr);
        System.out.println("\n返回参数:\n" + jiemStr + "\n");
    }

    /**
     * params:{title:标题, msg:内容, mod:类型, fileid:文件id, sortnum:优先级}
     */
    @Test
    public void testAddNoticeForCity() {
        System.out.println("添加市级公告");
        //封装参数
        Map<String,Object> params = new HashMap<>();
        params.put("title", "德玛西亚");
        params.put("msg", "其实你已经死了,只是你还没有发现而已");
        params.put("mod", "1");
        params.put("fileid", "");
        params.put("sortnum", "0");
        request.setContent(JSONObject.toJSONString(params).getBytes());
        //添加
        JSONObject rs = noticeController.addNoticeForCity(request, response);
        String jiamStr = (String) rs.get("response");
        String jiemStr = Encrypt.jiem(jiamStr);
        System.out.println("\n返回参数:\n" + jiemStr + "\n");
    }

    /**
     * params:{id:公告id}
     */
    @Test
    public void testCancelNotice(){
        System.out.println("删除公告");
        //封装参数
        request.setContent("{\"id\": \"a8d14df3-50a0-40f0-88a2-238b30d33c6a\"}".getBytes());
        //删除公告
        JSONObject rs = noticeController.cancelNotice(request, response);
        String jiamStr = (String) rs.get("response");
        String jiemStr = Encrypt.jiem(jiamStr);
        System.out.println("\n返回参数:\n" + jiemStr + "\n");
    }

    /**
     * params:{id:公告id}
     */
    @Test
    public void testGetNotice(){
        System.out.println("查看公告");
        //封装参数
        request.setContent("{\"id\": \"a8d14df3-50a0-40f0-88a2-238b30d33c6a\"}".getBytes());
        //查看公告
        JSONObject rs = noticeController.getNotice(request, response);
        String jiamStr = (String) rs.get("response");
        String jiemStr = Encrypt.jiem(jiamStr);
        System.out.println("\n返回参数:\n" + jiemStr + "\n");
    }

    /**
     * params:{title:标题, msg:内容, lasttime:发布时间, fileid:文件id, sortnum:优先级}
     */
    @Test
    public void testAddNoticeForDistrict(){
        System.out.println("添加区级公告");
        //封装参数
        Map<String,Object> params = new HashMap<>();
        params.put("title", "德玛西亚");
        params.put("msg", "");
        params.put("mod", "2");
        params.put("lasttime", Format.getDateTime());
        params.put("fileid", "");
        params.put("sortnum", "0");
        params.put("districtcode", "310116");
        request.setContent(JSONObject.toJSONString(params).getBytes());
        //添加
        JSONObject rs = noticeController.addNoticeForDistrict(request, response);
        String jiamStr = (String) rs.get("response");
        String jiemStr = Encrypt.jiem(jiamStr);
        System.out.println("\n返回参数:\n" + jiemStr + "\n");
    }


    private LoginInfo getLoginInfo() {
        LoginInfo loginInfo = new LoginInfo();
        loginInfo.setId("测试123");
        loginInfo.setRoleid("2");
        loginInfo.setDistrictcode("310116");
        loginInfo.setRoletype("2");
        loginInfo.setUsername("测试123");
        loginInfo.setRealname("junit测试管理员");
        return loginInfo;
    }
}
